<?php

/**
 * Description of thanhtoan
 *
 * @author sonUIT
 */
class Thanhtoan extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library("loadpage");
        $this->load->model("mhoadon");
        $this->load->model("mcthd");
        $this->load->library('cart');
         $this->load->library("autogenid");
    }

    public function index() {
        $lp = new loadpage();
        $lp->load(array("dathang"), null);
    }

    public function tt() {

        $hd = new Mhoadon();
        // $hd->set_mahd($hd->Max("MAHD")+1);
        $hd->set_matk($this->session->userdata('matk'));
        $hd->set_diachi($this->input->post('diachi'));
        $hd->set_ngay(date("Y-m-d H:i:s"));
        $hd->set_ngaygiao($this->input->post("ngaygiao"));
        $hd->set_sodt($this->input->post("sodt"));
        $hd->set_tennn($this->input->post("tennn"));
        $pre = 'HD';
        $beforeID = $hd->GetLastID();
        $genid = new autogenid();
        $mahd = $genid->genID($pre, $beforeID);
        $hd->set_mahd($mahd);
        if ($hd->Save()) {
         
            $giohang = $this->cart->contents();
            foreach ($giohang as $hang) {
                $cthd = new Mcthd();
                $cthd->set_giaban($hang['price']);
                $cthd->set_matp($hang['id']);
                $cthd->set_sl($hang['qty']);
                $cthd->set_mahd($hd->get_mahd());
                $cthd->Save();
            }
            
            $this->cart->destroy();
            header("location:".base_url("index.php/thanhtoan/inhd/?mahd=".$hd->get_mahd()));
        }
    }

    public function inhd() {
        if (!isset($_GET['mahd']) or ($_GET['mahd'] == "")) {
            $this->load->view('page404');
            return;
        } else {
            $mahd = $_GET['mahd'];
          //  echo 'hoadon';
            $hoadon = new Mhoadon();
            $hoadon->getObject('mahd="'.$mahd.'"');
            if ($hoadon->get_mahd() == "")
                echo 'ko ton tai';
            else {
                
                $this->load->model("mcthd");
                $cthd = new Mcthd();
                $dt = array();
                
                $dt['cthd'] = $cthd->getArrayCTHD($mahd);
                $dt['mahd'] = $mahd;
                $dt['hoten'] = $hoadon->get_tennn();
                $dt['diachi'] = $hoadon->get_diachi();
                $dt['ngaygiao'] = $hoadon->get_ngaygiao();
                $dt['ngaydat'] = $hoadon->get_ngaydat();
                $dt['sodt'] = $hoadon->get_sodt();
                //var_dump($dt['cthd']);
                //var_dump($dt['cthd']);
                ob_clean();
                $this->load->library('mpdf');
              
                $mpdf = new Pdf();
               
                $dt['mpdf'] = $mpdf;
                // $html = "Vo Minh";
                $mpdf->SetAutoFont(AUTOFONT_ALL);
               $mpdf->SetHeader('AIOBookStore.vn||{PAGENO}');
            $this->load->view("pdf_cthd", $dt);
                //$mpdf->Output();//
              //  $mpdf->Output ("file/mpdf.pdf", 'F');
               
               //exit();
            }
        }
    }
    public function lichsugd(){
        $matk= $this->session->userdata('matk');
        $hd=new Mhoadon();
        $data=array();
        $data['hoadon']=$hd->getListObject('*', 'matk="'.$matk.'"');
         $lp = new loadpage();
         $lp->load(array("lichsugd_view"), $data);
    }

}
?>
